using System;
using System.Linq;
using Core.Domain.Entities;
using Core.Domain.Interfaces;
using Core.IocHelpers;

namespace DataMain.Repositories
{
  [AutoRegister]
  public class PersonFinder : IPersonFinder
  {
    private IQueryable<Person> _people;

    #region IPersonFinder Members

    public IQueryable<Person> DataSource
    {
      set { _people = value; }
    }

    public Person ByNamePart(string namePart)
    {
      // TODO: glittle 2009-04-11 - If need to return in order, this could get more complex
      return (from p in _people
              where p.FirstName.StartsWith(namePart, StringComparison.CurrentCultureIgnoreCase)
              || p.LastName.StartsWith(namePart, StringComparison.CurrentCultureIgnoreCase)
              || p.OtherFirstNames.StartsWith(namePart, StringComparison.CurrentCultureIgnoreCase)
              || p.OtherLastNames.StartsWith(namePart, StringComparison.CurrentCultureIgnoreCase)
              select p)
              .FirstOrDefault();
    }

    #endregion
  }
}